package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.fz.us.base.util.LogUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.joint.base.entity.Comment;
import com.joint.base.entity.Company;
import com.joint.base.entity.Users;
import com.joint.base.mp.WxEchoMpServiceImpl;
import com.joint.base.mp.WxMpInCacheConfigStorage;
import com.joint.base.service.CommentService;
import com.joint.core.entity.Announce;
import com.joint.core.service.AnnounceService;
import com.joint.core.service.ProInfoService;
import com.joint.web.action.BaseController;
import fz.me.chanjar.weixin.common.bean.WxJsapiSignature;
import fz.me.chanjar.weixin.common.exception.WxErrorException;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 后台Action类 - 微信首页
 * ============================================================================
 * 版权所有 2014 min_xu。
 * ----------------------------------------------------------------------------
 *
 * @author min_xu
 *
 * @version 1.1 2014-3-31
 */
@Controller("mpIndex")
@RequestMapping("/mp")
public class IndexController extends BaseController {
	private static final long serialVersionUID = -5383463207248344967L;
    @Resource
    private CommentService commentService;
    @Resource
    private ProInfoService proInfoService;
    @Resource
    private AnnounceService announceService;
    @Resource
    private WxMpInCacheConfigStorage wxMpInCacheConfigStorage;
    @Resource
    private WxEchoMpServiceImpl wxMpService;


    private List<Map<String,Object>> msgList;
    private List<Announce> annonList;
    private WxJsapiSignature jsapiSignature;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
	public String execute(){
        try {
            jsapiSignature =  wxMpService.createJsapiSignature(getRequest().getRequestURL()+"");
            jsapiSignature.setAppId(wxMpInCacheConfigStorage.getAppId());
            //LogUtil.info("appId:" + jsapiSignature.getAppId() );
        } catch (WxErrorException e) {
            LogUtil.info(e);
        }
        return "/mp/index";
    }
    @RequestMapping(value = "/center",method = RequestMethod.GET)
    public String center(){
        return "/mp/index";
    }

    public String message(){
        pager = new Pager();
        rows = LISTVIEW_WEB_SIZE;
        pager.setPageSize(rows);
        pager.setPageNumber(page);

        Company company = usersService.getCompanyByUser();
        Users users = usersService.getLoginInfo();
        Map rmap = Maps.newHashMap();
        rmap.put("toUsers",users);
        pager = commentService.findByPagerAndCompany(pager,null,company,rmap);
        List<Comment> commentList = (List<Comment>) pager.getList();
        msgList = Lists.newArrayList();
        for(Comment comment : commentList){
            msgList.add(commentService.getListMap(comment));
        }
        return "message";

    }

    @RequestMapping(value = "index/notice",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> notice(int page,String pageKeyword){
        JSONObject data=new JSONObject();
        List<Map<String,Object>> dataRows=Lists.newArrayList();
        Map<String,Object> rmap;
        pager = new Pager();
        pager.setPageSize(10);
        pager.setPageNumber(page);
        Company company = usersService.getCompanyByUser();
        Map<String, Object> map;
        map = Maps.newHashMap();
        map.put("state", new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
        pager = announceService.findByPagerAndCompany(pager,null,company,map);
        annonList = (List<Announce>) pager.getList();
        String names="";
        for(Announce announce:annonList){
            rmap=Maps.newHashMap();
            rmap.put("url",getPath()+"mp/ajax-announce/read/"+announce.getId());
            rmap.put("name",announce.getName());
            rmap.put("content",announce.getContent());
            if(announce.getUsersSet().size()>0){
                for(Users users:announce.getUsersSet()){
                    names+=users.getName()+",";
                }
            }
            rmap.put("call", announce.getCallType() == 0 ? "全体成员" :names);
            rmap.put("createDate", DateFormatUtils.format(announce.getCreateDate(),"yyyy-MM-dd"));
            dataRows.add(rmap);
        }
        data.put("pageNum", pager.getPageNumber());//页数
        data.put("pageSize", dataRows.size());//记录数
        data.put("pageCount", pager.getPageCount());//总页数
        data.put("totalCount", pager.getTotalCount());//总记录数
        data.put("pageKeyword", pager.getKeyword()==null?"": pager.getKeyword());//查询
        data.put("dataRows", dataRows);//数据
        return  ajaxHtmlAppResult(1,"",data);
    }

    public List<Map<String, Object>> getMsgList() {
        return msgList;
    }

    public void setMsgList(List<Map<String, Object>> msgList) {
        this.msgList = msgList;
    }

    public List<Announce> getAnnonList() {
        return annonList;
    }

    public void setAnnonList(List<Announce> annonList) {
        this.annonList = annonList;
    }

    public WxJsapiSignature getJsapiSignature() {
        return jsapiSignature;
    }

    public void setJsapiSignature(WxJsapiSignature jsapiSignature) {
        this.jsapiSignature = jsapiSignature;
    }
}
